<?php

class Tasks extends CI_Controller {

    public function index() {
        $this->viewAll();
    }
    
    private function checkLevel() {
         if ($this->session->userdata('fk_role') != 2)
            return redirect(base_url() . '/tasks/', 'refresh');
    }
    
    public function add() {
       $this->checkLevel();
       $data = array('books' => $this->books_model->listAll());
        $this->load->view('templates/header.php');
        $this->load->view('pages/tasks/addTask.php', $data);
        $this->load->view('templates/footer.php');
        
         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $this->createTask( $this->input->post('name'), 
                    $this->input->post('fk_books'),
                    $this->input->post('x_units'), 
                    $this->input->post('y_units'), 
                     $this->input->post('description'),
                     $this->input->post('page'),
                     $this->input->post('functions_count'));
          
        }
    }

    public function createTask($name,$id_books,$x_units,$y_units,$description,$page,$functions_count){
          $this->checkLevel();
          $this->tasks_model->createTask($name, $id_books, $x_units, $y_units, $description, $page, $functions_count);
          $user_id = $this->session->userdata('id');
          $username = $this->session->userdata('firstname').' '.$this->session->userdata('lastname');
          $this->log_model->addLog($user_id,"Vytvorena nova uloha. Autor:" . $username . " Nazov: " . $name . " Popis: " . $description);
          return redirect(base_url() . '/tasks/', 'refresh');
    }

    // zobrazi vsetky zadania
    public function viewAll() {
          $data = array('tasks' => $this->tasks_model->listAll());
          
        $this->load->view('templates/header.php');
        $this->load->view('pages/tasks/viewAllTasks.php', $data);
        $this->load->view('templates/footer.php');
    }

    // zobrazi zadanie, ID je treti parameter z adresy 
    public function view(){
        $id = $this->uri->segment(3);
        $data = array();
        $data['task'] = $this->tasks_model->getByIdJoined($id);
        if ($this->session->userdata('logged')) {
            $user_id = $this->session->userdata('id');
            $username = $this->session->userdata('firstname').' '.$this->session->userdata('lastname');
            $this->log_model->addLog($user_id, $username . " si prezera ulohu: " . $data['task']->name);
        }

        $this->load->view('templates/header.php');
        $this->load->view('pages/tasks/viewTask.php',$data);
        $this->load->view('templates/footer.php');
    }

    // ponukne editaciu zadania, ID je treti parameter adresy
    public function edit(){
         $id = $this->uri->segment(3);
         
         $data = array();
         $data['task'] = $this->tasks_model->getByIdJoined($id);
         $fk_user = $this->session->userdata('id');
         // nacitame solution ktore ma
         if (!empty($fk_user)) {
           $data['filled']  = $this->solution_model->get($fk_user, $id)->data;
         }


        $this->load->view('templates/header.php');
        $this->load->view('pages/tasks/editTask.php',$data);
        $this->load->view('templates/footer.php');
    }
    
    public function editSave() {
         // ak submitujeme (cez AJAX) tak uloz
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $jsonized = $_POST['json'];

            $fk_user = $this->session->userdata('id');
            $task_id = $_POST['task_id'];
            
            if (!empty($fk_user)) { // je prihlaseny
                //zaloguj
                $user_id = $this->session->userdata('id');
                $username = $this->session->userdata('firstname').' '.$this->session->userdata('lastname');
                $this->log_model->addLog($user_id, $username . " ulozil riesenie pre ulohu: " . $this->tasks_model->getByIdJoined($task_id)->name);

                $this->solution_model->createEdit($fk_user, $task_id, $jsonized);
            }
        }
    }

    // vrati PDFko, pokusi sa zobrazit ak vie
    public function toPdf() {
        return $this->_toPdf($this->uri->segment(3));
    }
    
    private function _toPdf($id_task, $download=""){
        header('Content-type: application/pdf');
        $this->lib_pdf->getTask($id_task, $download);
    }
    
    // vrati vyplnene zadanie
    public function solutionToPdf() {
        $id = $this->uri->segment(3);
        if (empty($id)) {
            return;
        }

        $data = urldecode($this->uri->segment(4));
        $userId = $this->session->userdata('id');
        // ak data niesu poslane ako 4. parameter, tak skus vytiahnut z databazy
        if (empty($data)) {
            $data = $this->solution_model->get($userId, $id)->data;
        }

        // ak i teraz nemame data, je koniec
        if (empty($data)) {
            return;
        }

        return $this->_solutionToPdf($id, $data);

    }


    private function _solutionToPdf($id_user,$id_task){
        $this->lib_pdf->getSolution($id_user, $id_task);
    }

    
    // ajaxovo volana, vytiahne si zo segmentu ID tasku, a data z postu
    public function toGraph(){
        $task_id = $this->uri->segment(3);
        $data = urldecode($this->uri->segment(4));
        
        $this->lib_phplot->getGraph($task_id, $data);
    }

    public function deleteTask() {
        $this->checkLevel();
        $id = $this->uri->segment(3);
        $this->delete($id);
    }
    
    private function delete($id){
       $this->checkLevel();

       $user_id = $this->session->userdata('id');
       $username = $this->session->userdata('firstname').' '.$this->session->userdata('lastname');
       $this->log_model->addLog($user_id, $username . " vymazal ulohu " . $this->tasks_model->getById($id)->name);

       $this->solution_model->deleteAll($id); // vymaze vsetky riesenia
       $this->tasks_model->deleteTask($id); // vymaze ulohu

       return redirect(base_url() . '/tasks/', 'refresh');
    }





}



?>